<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="应用名称" prop="name">
				<el-input v-model="form.name" clearable placeholder="应用名称" maxlenght="20"></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sortNo">
				<el-input-number v-model="form.sortNo" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="所属分类" prop="parentId">
				<sc-select v-model="form.parentId" :apiObj="$API.chat.appConfig.getAppParentListForSelect" clearable filterable style="width: 100%"></sc-select>
			</el-form-item>
			<el-form-item label="图标" prop="icon">
				<sc-icon-select v-model="form.icon" clearable></sc-icon-select>
			</el-form-item>
			<el-form-item label="路由地址" prop="path">
				<el-input v-model="form.path" clearable placeholder="路由地址"></el-input>
			</el-form-item>
			<el-form-item label="视图" prop="component">
				<el-input v-model="form.component" clearable placeholder="视图">
					<template #prepend>views/</template>
				</el-input>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status"  active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="是否显示" prop="hidden">
				<el-switch v-model="form.hidden"  active-value="0" inactive-value="1"></el-switch>
			</el-form-item>
			<el-form-item label="应用说明" prop="remark">
				<el-input v-model="form.remark" clearable placeholder="应用说明" type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import scIconSelect from '@/components/scIconSelect'
	export default {
		emits: ['success', 'closed'],
		components: {
			scIconSelect
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增APP',
					edit: '编辑APP'
				},
				form: {
					appId: "",
					name: "",
					sortNo:0,
					parentId: "",
					icon: "",
					path: "",
					status:'1',
					hidden:"0",
					component: "",
					remark:"",
				},
				rules: {
					appId:[
						{required: true, message: '请输入应用标识'}
					],
					parentId:[
						{required: true, message: '请选择APP就用分类'}
					],
					name:[
						{required: true, message: '请输入应用名称'}
					],
					icon:[
						{required: true, message: '请选择应用图标'}
					],
					path:[
						{required: true, message: '路由地址不能为空'}
					],
					component:[
						{required: true, message: '视图不能为空', trigger: 'change'}
					],
				},
				visible: false,
				isSaveing: false
			}
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res;
						if (this.mode == "add") {
							res = await this.$API.chat.appConfig.insertAppConfig.post(this.form);
						} else if (this.mode == "edit") {
							res = await this.$API.chat.appConfig.updateAppConfig.post(this.form);
						}
						this.isSaveing = false;
						if (res.code == 200) {
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.appId = data.appId
				this.form.name = data.name
				this.form.sortNo = data.sortNo
				this.form.parentId = data.parentId
				this.form.icon = data.icon
				this.form.path = data.path
				this.form.status=data.status
				this.form.hidden = data.hidden
				this.form.component = data.component
				this.form.remark = data.remark;
			}
		}
	}
</script>

<style>
</style>
